package com.dayatang.android.Db;

import com.dayatang.android.config.Config;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {
	
	private static final String DATABASENAME = Config.DBNAME;
	private static final int DATABASEVERSION = 1;

	public DBOpenHelper(Context context) {
		super(context, DATABASENAME, null, DATABASEVERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE project_search_log (id integer primary key autoincrement,project_name varchar(2000),output varchar(200),projectid varchar(200),budget varchar(200))");
		db.execSQL("CREATE TABLE sub_project (id integer primary key autoincrement,name varchar(200),start_date varchar(200),finish_date varchar(200),project_type varchar(200),area varchar(200),responsible_division varchar(200),output varchar(200),owner_info varchar(200),supervisor_info varchar(200),designer_info varchar(200),projectid varchar(200),subprojectid varchar(200),ap_type varchar(200),delist_code varchar(200),delisting_entrust_type varchar(200),address varchar(200),option_cable_finish_date varchar(200),workitem_quantity text,construction_team text,specialty_output text,transmission_open_date varchar(200),focus varchar(200),progress_info text,budget varchar(200))");
		db.execSQL("CREATE TABLE projects (id integer primary key autoincrement,name varchar(200),start_date varchar(200),finish_date varchar(200),project_type varchar(200),area varchar(200),responsible_division varchar(200),output varchar(200),owner_info varchar(200),supervisor_info varchar(200),designer_info varchar(200),projectid varchar(200),contract_number varchar(200),sub_project_num varchar(200),sub_project_finished_num varchar(200),sub_project_nofinished_num text,workitem_quantity text,specialty_output text,budget varchar(200),expenditure varchar(200),receipt varchar(200),status varchar(200))");
		db.execSQL("CREATE TABLE dictionarys (id integer primary key autoincrement,name varchar(200),thisid varchar(200),type varchar(200))");
		db.execSQL("CREATE TABLE persons (id integer primary key autoincrement,name varchar(200),organization_name varchar(200),holderofan_office varchar(200),cell_phone varchar(2000))");
		db.execSQL("CREATE TABLE system_messages (id integer primary key autoincrement,title varchar(200),content text,organization_name varchar(200),release_date varchar(200),thisid varchar(200),already_read varchar(10))");
		db.execSQL("CREATE TABLE newest_id (id integer primary key autoincrement,newid varchar(10))");
		insertInit(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS project_search_log");
		db.execSQL("DROP TABLE IF EXISTS sub_project");
		db.execSQL("DROP TABLE IF EXISTS projects");
		db.execSQL("DROP TABLE IF EXISTS dictionarys");
		db.execSQL("DROP TABLE IF EXISTS persons");
		db.execSQL("DROP TABLE IF EXISTS system_messages");
		db.execSQL("DROP TABLE IF EXISTS newest_id");
		onCreate(db);
	}
	
	private void insertInit(SQLiteDatabase db){
		ContentValues values = new ContentValues();
		values.put("newid", "0");
		db.insert("newest_id", null, values);
	}
}
